|
|
|
|
|
|
|
in your data table. If book B has sales for four quarters, you have four rows, and so on. How can you produce a view that lists the quarters as columns instead of as rows? |
|
|
|
|
|
|
|
|
You can accomplish this with a complicated set of subsequent SQL statements that produces temporary views, merges them together, and so on. Thanks to the folks who invented the Microsoft Access Jet database engine, however, you can use the TRANSFORM_PIVOT statement instead. You can produce the entire result set in one SQL statement using TRANSFORM_PIVOT. The following SQL statement shows how this can be done. See Figure 8.33 for a sample result set. |
|
|
|
|
|
|
|
|
TRANSFORM SUM(BookSales.Sales)
SELECT Title FROM BookSales
GROUP BY Title
PIVOT BookSales.Qtr |
|
|
|
|
|
|
|
|
Figure 8.33.
The TRANSFORM_PIVOT example. |
|
|
|
|
|
|
|
|
Notice the form of the TRANSFORM_PIVOT statement. It starts with the TRANSFORM keyword, not the SELECT keyword. Notice that a single SQL aggregate function immediately follows the TRANSFORM keyword. This is required, even if no real totaling will be performed. After the TRANSFORM aggregate function clause, you have the standard SELECT_FROM clause. Notice that the preceding example did not include the Booksales.Sales column in the SELECT statement because it will be produced by the TRANSFORM_PIVOT clause automatically. The GROUP BY clause is required in order to tell SQL how to treat the successive rows that will be handled for each BookSales.Title. Finally, add the PIVOT keyword, followed by the column that you want to use, as the set of headers that follow out to the right of the GROUP BY clause. |
|
|
|
|
|
|
|
|
TRANSFORM_PIVOT uses the data in the PIVOT column as column headers in the result set. You will have as many columns in your result set as you have unique values in your PIVOT |
|
|
|
|
|